package br.com.sge.servicosimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.sge.entidades.Aluno;
import br.com.sge.entidades.Disciplina;
import br.com.sge.entidades.Matricula;
import br.com.sge.servicos.MatriculaServico;
import br.com.sge.util.HibernateUtil;

/**
 *
 *@author Gustavo Rodrigues Torre
 */
public class MatriculaServicoImp implements MatriculaServico {

    public void save(Matricula matricula) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.save(matricula);
        t.commit();
    }
    public Matricula getMatricula(long id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Matricula) session.load(Matricula.class, id);
    }
    
    public Disciplina getDisciplina(long id) {
    	 Session session = HibernateUtil.getSessionFactory().openSession();
         return (Disciplina) session.load(Disciplina.class, id);
    }
    
    public Aluno getAluno(long id) {
   	 Session session = HibernateUtil.getSessionFactory().openSession();
        return (Aluno) session.load(Aluno.class, id);
   }
    
    public List<Matricula> list() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        List lista = session.createQuery("from Matricula ma inner join fetch ma.Aluno alu inner join fetch alu.Disciplina dis").list();
        t.commit();
        return lista;
        
        //Join
        /*
        from Tabela1 t1   
        inner join fetch t1.tabela2 t2  
        inner join fetch t2.tabela3 t3
        */  
    }
       
    public void remove(Matricula matricula) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(matricula);
        t.commit();
    }
    public void update(Matricula matricula) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.update(matricula);
        t.commit();
    }
}
